package descentFly.away3D4.chips
{
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.loaders.Loader3D;
	import away3d.materials.ColorMaterial;
	import away3d.tools.Explode;
	import away3d.tools.MeshHelper;
	import away3d.tools.utils.Bounds;
	
	import descentFly.away3D4.action.df_action;
	import descentFly.away3D4.action.df_modelActions;
	import descentFly.away3D4.chips.body.JChipBox;
	import descentFly.away3D4.chips.body.JChipSphere;
	import descentFly.away3D4.df_MeshHelper;
	import descentFly.chips.df_chip;
	import descentFly.df_space;
	
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	
	import jiglib.geometry.JBox;
	import jiglib.math.JMatrix3D;
	import jiglib.physics.RigidBody;
	import jiglib.plugin.away3d4.Away3D4Mesh;
	
	public class df_chipModel extends df_chipAway {
		public static const WEAPONT_ACTIVATE_NAME	: String 	= 'wpActive';
		public static const WEAPONT_DEACTIVATE_NAME	: String = 'wpDeActive';
		
		public static const ACTION_TIMER			: String = 'wpTimer';
		
		private var _model		: ObjectContainer3D;
		private var _actions	: df_modelActions;
		public function df_chipModel(a_space:df_space, a_options:Object) {
			super(a_space, a_options);
		}
		
		override protected function initialize():void {
			if (!_model) initModel(options.model);
			else super.initialize();
		}
		
		public function get model(): ObjectContainer3D {
			return _model;
		}
		
		protected function initModel(a_model: ObjectContainer3D):void {
			_model = a_model;
			if (_model) {
				if (options.box) resizeFromBox(options.box);
				if (options[df_chip.FIELD_MODELDATA]) parseModelData(options[df_chip.FIELD_MODELDATA]);
				initialize();
			}
		}
		
		protected function resizeFromBox(a_box: Vector3D): void {
			
			Bounds.getObjectContainerBounds(_model);
			var scale : Number = Math.max(a_box.x/Bounds.width, Math.max(a_box.y/Bounds.height, a_box.z/Bounds.depth));
			df_MeshHelper.scaleObject(_model, scale);
		}
		
		override protected function get awayObject(): ObjectContainer3D {
			return model;
		}
		
		override protected function getTransform(): Matrix3D {
			return model.transform;
		}
		
		override protected function setTransform(a_transform: Matrix3D): void {
			model.transform = a_transform;
			super.setTransform(a_transform);
		}
		
		override protected function initBody():void {
			var box: RigidBody = new JChipSphere(model, new df_chipSkin(this));
//			var box: RigidBody = new JChipBox(model, new df_chipSkin(this));
			addModelInScene(model);
			setBody(box);
		}
		
/*		override public function get attackableRadius(): Number {
			return (body as JChipSphere).radius;
		}*/
		
		protected function addModelInScene(a_model: ObjectContainer3D): void {
			spaceAway.view.view.scene.addChild(_model);
		}
		
		protected function parseModelData(modelData: Object): void {
			for (var prop: String in modelData)
				this['_parse' + prop](modelData[prop]);
		}
		
		protected function _parseActions(a_actions: Array): void {
			_actions = new df_modelActions(a_actions, _model);
		}
		
		public function beginAction(actionName: String, startParam: Object=null): void {
			_actions.beginAction(actionName, startParam);
		}
		
		override protected function update():void {
			super.update();
			if (_model && body) _model.transform = body.skin.transform;
		}
		
		override public function dispose():void {
			if (_actions) _actions.dispose();
			super.dispose();
		}
	}
}